package com.freesun.shop.controller;


import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.upload.FastFile;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/admin/file")
public class FileController {

    @Autowired
    private FastFileStorageClient fileStorageClient;
    @ApiOperation("文件的上传")
    @PostMapping("/upload/element")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        FastFile.Builder builder = new FastFile.Builder();
        String filename = file.getOriginalFilename();
        builder.withFile(file.getInputStream(),file.getSize(),filename.substring(filename.lastIndexOf(".")+1));
        StorePath storePath = fileStorageClient.uploadFile(builder.build());
        return ResponseEntity.ok(storePath.getFullPath());
    }
}
